/**   
* @Title: CrunchifyAPI.java 
* @Package: com.ifuntimes.http
* @Description: TODO
* @author:YuHai
* @Email: it@ifuntimes.com  
* @date 2017年09月05日 下午10:42:02 
* @version V1.0   
*/
package com.ifuntimes.http;

import java.net.URLDecoder;

import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * @ClassName: CrunchifyAPI
 * @Description:
 * @author YuHai it@ifuntimes.com
 * @date 2017年09月05日 下午10:42:02
 * 
 */

@Path("v1/cloud")
public class CrunchifyAPI {

	private final static Logger logger = LoggerFactory.getLogger(CrunchifyAPI.class);
	private final Crunchify crunchify;

	/**
	 * <p>
	 * Title:
	 * </p>
	 * <p>
	 * Description:
	 * </p>
	 */
	public CrunchifyAPI() {
		crunchify = new CrunchifyImpl();
	}

	/**
	 * 
	 * @Title: getAccountInfo
	 * @Description: 获取username对应的账户信息 ；如果是来自MCC Token的请求，则返回target对应的账户信息。
	 * @return Response
	 */
	@Path("/AccountService/getAccountInfo")
	@POST
	@Produces(MediaType.APPLICATION_JSON)
	public Response getAccountInfo(@HeaderParam("opUsername")String opUsername, @HeaderParam("opPassword")String opPassword,
			@HeaderParam("tgUsername")String tgUsername, @HeaderParam("tgPassword")String tgPassword,@HeaderParam("tgSubname")String tgSubname,
			String data) {
		try {		
			opUsername=URLDecoder.decode(opUsername,"UTF-8");
			tgUsername=URLDecoder.decode(tgUsername,"UTF-8");
			tgSubname=URLDecoder.decode(tgSubname,"UTF-8");	
			logger.debug("username:"+opUsername);
			return Response.status(200).entity(crunchify.getAccountInfo(opUsername,opPassword,tgUsername,tgPassword,tgSubname,data).toString()).build();
		} catch (Exception e) {
			logger.error(e.getMessage());
			return Response.status(500).entity(crunchify.serverError(e).toString()).build();
		}
	}
	
	
	/**
	 * 
	 * @Title: updateAccountInfo
	 * @Description:  更新username对应的账户信息，如果是来自MCC Token的请求，则更新target对应的账户信息。
	 * @return Response
	 */
	@Path("/AccountService/updateAccountInfo")
	@POST
	@Produces(MediaType.APPLICATION_JSON)
	public Response updateAccountInfo(@HeaderParam("opUsername")String opUsername, @HeaderParam("opPassword")String opPassword,
			@HeaderParam("tgUsername")String tgUsername, @HeaderParam("tgPassword")String tgPassword,@HeaderParam("tgSubname")String tgSubname,
			String data) {
		try {			
			opUsername=URLDecoder.decode(opUsername,"UTF-8");
			tgUsername=URLDecoder.decode(tgUsername,"UTF-8");
			tgSubname=URLDecoder.decode(tgSubname,"UTF-8");			
			return Response.status(200).entity(crunchify.updateAccountInfo(opUsername,opPassword,tgUsername,tgPassword,tgSubname,data).toString()).build();
		} catch (Exception e) {
			logger.error(e.getMessage());
			return Response.status(500).entity(crunchify.serverError(e).toString()).build();
		}
	}
	
	
	
	/**
	 * 
	 * @Title: getCampaign
	 * @Description:  根据指定的计划id获取推广计划(id可批量) 。
	 * @return Response
	 */
	@Path("/CampaignService/getCampaign")
	@POST
	@Produces(MediaType.APPLICATION_JSON)
	public Response getCampaign(@HeaderParam("opUsername")String opUsername, @HeaderParam("opPassword")String opPassword,
			@HeaderParam("tgUsername")String tgUsername, @HeaderParam("tgPassword")String tgPassword,@HeaderParam("tgSubname")String tgSubname,
			String data) {
		try {			
			opUsername=URLDecoder.decode(opUsername,"UTF-8");
			tgUsername=URLDecoder.decode(tgUsername,"UTF-8");
			tgSubname=URLDecoder.decode(tgSubname,"UTF-8");				
			return Response.status(200).entity(crunchify.getCampaign(opUsername, opPassword, tgUsername, tgPassword, tgSubname, data).toString()).build();
		} catch (Exception e) {
			logger.error(e.getMessage());
			return Response.status(500).entity(crunchify.serverError(e).toString()).build();
		}
	}
	 
	
	/**
	 * 
	 * @Title: getAdgroup
	 * @Description:  根据指定的单元id获取推广单元。
	 * @return Response
	 */
	@Path("/AdgroupService/getAdgroup")
	@POST
	@Produces(MediaType.APPLICATION_JSON)
	public Response getAdgroup(@HeaderParam("opUsername")String opUsername, @HeaderParam("opPassword")String opPassword,
			@HeaderParam("tgUsername")String tgUsername, @HeaderParam("tgPassword")String tgPassword,@HeaderParam("tgSubname")String tgSubname,
			String data) {
		try {			
			opUsername=URLDecoder.decode(opUsername,"UTF-8");
			tgUsername=URLDecoder.decode(tgUsername,"UTF-8");
			tgSubname=URLDecoder.decode(tgSubname,"UTF-8");			
			return Response.status(200).entity(crunchify.getAdgroup(opUsername, opPassword, tgUsername, tgPassword, tgSubname, data).toString()).build();
		} catch (Exception e) {
			logger.error(e.getMessage());
			return Response.status(500).entity(crunchify.serverError(e).toString()).build();
		}
	}
	
	/**
	 * 
	 * @Title: getAdgroup
	 * @Description:  根据指定的单元id获取推广单元。
	 * @return Response
	 */
	@Path("/KeywordService/getWord")
	@POST
	@Produces(MediaType.APPLICATION_JSON)
	public Response getWord(@HeaderParam("opUsername")String opUsername, @HeaderParam("opPassword")String opPassword,
			@HeaderParam("tgUsername")String tgUsername, @HeaderParam("tgPassword")String tgPassword,@HeaderParam("tgSubname")String tgSubname,
			String data) {
		try {			
			opUsername=URLDecoder.decode(opUsername,"UTF-8");
			tgUsername=URLDecoder.decode(tgUsername,"UTF-8");
			tgSubname=URLDecoder.decode(tgSubname,"UTF-8");			
			return Response.status(200).entity(crunchify.getWord(opUsername, opPassword, tgUsername, tgPassword, tgSubname, data).toString()).build();
		} catch (Exception e) {
			logger.error(e.getMessage());
			return Response.status(500).entity(crunchify.serverError(e).toString()).build();
		}
	}
}